/*
 * Wt12FileHandler.cs - Wordtrainer
 * 
 * Copyright (C) 1998-2008 Markus Kilås
 *               
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 * 
 * Authors:
 *   Markus Kilås <markus@markuspage.com>
 *
 */

using System;
using System.Collections;
using Gtk;
using Mono.Unix;

namespace WtSharp
{
	
	
	public class Wt12FileHandler : IFileHandler
	{
		
		public Wt12FileHandler()
		{
		}
		
		public FileFilter[] FileFilters
		{
			get
			{
				FileFilter filter1 = new FileFilter();
				filter1.AddPattern("*.wt*");
				filter1.Name = Catalog.GetString("Wordtrainer-files (.wt*)");

				FileFilter filter2 = new FileFilter();
				filter2.AddPattern("*.wt1");
				filter2.Name = "Wordtrainer 1.0 (.wt1)";
				
				FileFilter filter3 = new FileFilter();
				filter3.AddPattern("*.wt2");
				filter3.Name = "Wordtrainer 2.x-3.0 (.wt2)";
				
				return new FileFilter[]{filter1,filter2,filter3};
			}
		}

		public WtDocument Open(string Filename)
		{
			System.IO.TextReader tr = new System.IO.StreamReader(Filename);

			WtDocument doc = new WtDocument(Filename);
						
			string line;
			const int INFO = 1;
			const int MARKS = 2;
			const int WORDS = 3;
			int section = 0;
			
			string wordF = "";
			string wordT = "";
			
			Hashtable marks = new Hashtable();
			
			while((line = tr.ReadLine()) != null)
			{	
				if(line.Length < 1)
					continue;
				
				if(line.StartsWith("["))
				{
					string sect = line.Substring(1, line.Length-2);
					
					if(sect.Equals("INFO"))
						section = INFO;
					else if(sect.Equals("MARKS"))
						section = MARKS;
					else if(sect.Equals("WORDS"))
						section = WORDS;
					else
						section = 0;
				}
				else {
					
					string[] val = line.Split('=');
					
					if(section == INFO)
					{
						if(doc.Info.Contains(val[0]))
							doc.Info[val[0]] = val[1];
						else
							doc.Info.Add(val[0], val[1]);
					}
					else if(section == MARKS)
					{
						marks.Add(Int32.Parse(val[0].Substring(0, val[0].Length-1)), Int32.Parse(val[1]));
					}
					else if(section == WORDS)
					{
						if("F".Equals(val[0]))
							doc.TitleForeign = val[1];
						else if("O".Equals(val[0]))
							doc.TitleTranslation = val[1];
						else
						{
							if(val[0].IndexOf("B") != -1 || val[0].IndexOf("T") != -1)
							{
								wordT = val[1];
								doc.Words.Add(new WtWord(wordF, wordT));
								wordF = wordT = "";
							}
							else
							{								
								wordF = val[1];
							}
						}
					}
				}
				
			}
			
			// Apply marks
			foreach(System.Collections.DictionaryEntry item in marks)
			{
				Console.WriteLine(item.Key+"="+item.Value);
				((WtWord)doc.Words[(int)item.Key-1]).Type = (int)item.Value;
			}
			
			// Fix newlines in description
			string desc = (string)doc.Info["DESCRIPTION"];
			if(desc != null)
				doc.Info["DESCRIPTION"] = desc.Replace((char)30, '\n').Replace((char)31, '\r');
			else
				doc.Info["DESCRIPTION"] = "";
			
			tr.Close();
			doc.Changed = false;
			doc.New = false;
			
			return doc;
		}
		
		public void Save(WtDocument doc, string filename)
		{
			System.IO.TextWriter tw = new System.IO.StreamWriter(filename);
			
			Console.WriteLine(doc.Info["CREATOR"]);
			
			doc.Info["VERSION"] = "2.0";
			doc.Info["CREATOR"] = "Wordtrainer " + AboutDialog.VERSION_STRING;
			doc.Info["ANTAL"] = doc.Words.Count;
			
			// Fix newlines
			string origDesc = (string)doc.Info["DESCRIPTION"];
			doc.Info["DESCRIPTION"] = origDesc.Replace('\n', (char)30).Replace('\r', (char)31);
			
			if(doc.Info["TITLE"] == null || (string)doc.Info["TITLE"] == "")
				doc.Info["TITLE"] = doc.FileTitle;
			
			tw.WriteLine("[INFO]");
			foreach(System.Collections.DictionaryEntry item in doc.Info)
			{
				tw.WriteLine(item.Key+"="+item.Value);
			}
			tw.WriteLine();
			doc.Info["DESCRIPTION"] = origDesc; // Fix newlines
			
			tw.WriteLine("[MARKS]");
			int index = 0;
			foreach(WtWord word in doc.Words)
			{
				index++;
				if(word.Type != 0)
					tw.WriteLine(index+"M="+word.Type);
			}
			tw.WriteLine();
			
			tw.WriteLine("[WORDS]");
			tw.WriteLine("F="+doc.TitleForeign);
			tw.WriteLine("O="+doc.TitleTranslation);
			index = 0;
			foreach(WtWord word in doc.Words)
			{
				index++;
				tw.WriteLine(index+"="+word.WordF);
				tw.WriteLine(index+"B="+word.WordT);
			}
			
			tw.Close();
			doc.Changed = false;
			doc.New = false;
			doc.FileName = filename;
			doc.FileTitle = WtDocument.GetFileTitle(filename);
		}
		
	}
}
